package com.ql.controller;

import com.ql.domain.CementMixing;
import com.ql.domain.EarlyWarningChart;
import com.ql.service.serviceImpl.ExcelServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.w3c.dom.stylesheets.LinkStyle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/data")
public class DataController {
    @Autowired
    ExcelServiceImpl excelService;

    @RequestMapping("/getVisitReportData.do")
    @ResponseBody
    public List getVisitReportData() {
        ArrayList<EarlyWarningChart> arrayList = new ArrayList<>();
        EarlyWarningChart chart = new EarlyWarningChart("2021-04-01", "21", "45");
        EarlyWarningChart chart1 = new EarlyWarningChart("2021-04-02", "10", "60");
        EarlyWarningChart chart2 = new EarlyWarningChart("2021-04-03", "20", "30");
        EarlyWarningChart chart3 = new EarlyWarningChart("2021-04-04", "50", "50");
        EarlyWarningChart chart4 = new EarlyWarningChart("2021-04-05", "90", "10");
        EarlyWarningChart chart5 = new EarlyWarningChart("2021-04-06", "70", "20");
        arrayList.add(chart);
        arrayList.add(chart2);
        arrayList.add(chart3);
        arrayList.add(chart4);
        arrayList.add(chart5);
        arrayList.add(chart1);
        return arrayList;
    }

    @PostMapping("/getCategory.do")
    @ResponseBody
    public Map getCategory(String stake_mark) throws InterruptedException {
        System.out.println(stake_mark);
        Map<String, List> map = new HashMap<>();
        String[] arr = {"05:13:18", "05:13:19", "05:13:20", "05:13:21", "05:13:22", "05:13:23", "05:13:24", "05:13:25", "05:13:26", "05:13:27", "05:13:28", "05:13:29", "05:13:30", "05:13:31", "05:13:32", "05:13:33", "05:13:34", "05:13:35", "05:13:36", "05:13:37", "05:13:38", "05:13:39", "05:13:40", "05:13:41", "05:13:42", "05:13:43", "05:13:44", "05:13:45", "05:13:46", "05:13:47", "05:13:48", "05:13:49", "05:13:50", "05:13:51", "05:13:52", "05:13:53", "05:13:54", "05:13:55", "05:13:56", "05:13:57", "05:13:58", "05:13:59", "05:14:00", "05:14:01", "05:14:02", "05:14:03", "05:14:04", "05:14:05", "05:14:06", "05:14:07"};
        List<String> stringList = Stream.of(arr).collect(Collectors.toList());
        map.put("list1", stringList);
        List<String> list2 = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            int add = (int) (100 * Math.random());
            String s = String.valueOf(add);
            list2.add(s);
        }
        List<String> list3 = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            int add = (int) (1000 * Math.random());
            String s = String.valueOf(add);
            list3.add(s);
        }
        List<String> list4 = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            int add = (int) (10 * Math.random());
            String s = String.valueOf(add);
            list4.add(s);
        }
        List<String> list5 = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            int add = (int) (50 * Math.random());
            String s = String.valueOf(add);
            list5.add(s);
        }
        List<String> list6 = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            int add = (int) (25 * Math.random());
            String s = String.valueOf(add);
            list6.add(s);
        }
        List<String> list7 = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            int add = (int) (10 * Math.random());
            String s = String.valueOf(add);
            list7.add(s);
        }
        map.put("list2", list2);
        map.put("list3", list3);
        map.put("list4", list4);
        map.put("list5", list5);
        map.put("list6", list6);
        map.put("list7", list7);
        return map;
    }

    @RequestMapping("/excel/export.do")
    public void exportExcelCommon(HttpServletRequest request, HttpServletResponse response) throws IOException {
        OutputStream outputStream = null;
        try {
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("通用参数列表", "UTF-8") + ".xls");
            HSSFWorkbook wb = excelService.export();
            outputStream = response.getOutputStream();
            wb.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            outputStream.flush();
            outputStream.close();
        }
    }


    @RequestMapping("/getData.do")
    @ResponseBody
    public Map get() {
        Map<String, List> map = new HashMap<>();
        List<CementMixing> list = new ArrayList<>();
        List<CementMixing> list1 = new ArrayList<>();
        list.add(new CementMixing("1.01", "6.17", "0.89", "80.01","22.23"));
        list.add(new CementMixing("1.11", "5.57", "0.91", "85","23.32"));
        list.add(new CementMixing("1.23", "5.46", "0.90", "87","25.56"));
        list.add(new CementMixing("1.34", "3.58", "0.92", "90","22.33"));
        list.add(new CementMixing("3.62", "1.72", "0.89", "92","24.68"));
        list.add(new CementMixing("4.30", "7.97", "0.90", "111","27.89"));

        list1.add(new CementMixing("4.30", "7.97", "0.90", "111","27.89"));
        list1.add(new CementMixing("3.62", "1.72", "0.89", "92","24.68"));
        list1.add(new CementMixing("1.34", "3.58", "0.92", "90","22.33"));
        list1.add(new CementMixing("1.23", "5.46", "0.90", "87","25.56"));
        list1.add(new CementMixing("1.11", "5.57", "0.91", "85","23.32"));
        list1.add(new CementMixing("1.01", "6.17", "0.89", "80.01","22.23"));

        map.put("up",list);
        map.put("down",list1);
        return map;
    }

}
